
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Comedy Option
 */
public class FloorView extends JPanel{
    ArrayList<RoomView> roomViews;
    Floor repFloor;
    RoomView currentRoom = null;
    
    public FloorView(Floor toRep){
        roomViews = new ArrayList<RoomView>();
        repFloor = toRep;                
        this.setBackground(Color.white);
        this.addMouseListener(new RoomClickHandler());

    }
    
    public void addRoomView(RoomView newView){
        roomViews.add(newView);
        this.repaint();
    }
    
    public void removeView(RoomView toRemove){
        roomViews.remove(toRemove);
        this.repaint();
    }
    
    public RoomView findRoom(Point p){
        RoomView toCheck;
        int checkHeight;
        int checkWidth;
        int checkX;
        int checkY;
        double pointX = p.getX();
        double pointY = p.getY();
        for(int i = 0; i < roomViews.size(); i++){
            toCheck = roomViews.get(i);
            checkHeight = toCheck.getHeight();
            checkWidth = toCheck.getWidth();
            checkX = toCheck.getX();
            checkY= toCheck.getY();
            if((pointX >= checkX) && (pointX <= (checkX + checkWidth))
                    && (pointY >= checkY) && (pointY <= (checkY + checkHeight)))
            {
                return toCheck;
            }
        }
        return null;
    }
    
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        int height;
        int width;
        int xCoord;
        int yCoord;
        for(int i = 0; i < roomViews.size(); i++){
            RoomView toPaint = roomViews.get(i);
            height = toPaint.getHeight();
            width = toPaint.getWidth();
            xCoord = toPaint.getX();
            yCoord = toPaint.getY();
            Rectangle2D rectangle = new Rectangle2D.Double(xCoord, yCoord, width, height);
            g2.setColor(toPaint.getBgColor());
            g2.fill(rectangle);
            g2.setColor(Color.black);
            g2.draw(rectangle);
            g2.drawString("   " +toPaint.getRepRoom().getName(),xCoord + ((width/7) * 3), yCoord + (height/4));
            roomViews.get(i).decideImage();
            BufferedImage toDraw = roomViews.get(i).getImage();
            g.drawImage(toDraw, xCoord + ((width/7) *3), yCoord + (height/3), this);
        }
    }
    
    public Floor getFloor(){
        return repFloor;
    }
}
